package edu.cuit.avatar;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * java ReplaceText sourceFile targetFile oldString newString
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/11 16:07
 */
public class ReplaceText {
    public static void main(String[] args) {
        if(args.length != 4)
            throw new IllegalArgumentException("usage: java ReplaceText sourceFile targetFile oldString newString");

        var sourceFile = new File(args[0]);
        if(!sourceFile.exists())
            throw new IllegalStateException("source file " + args[0] + " does not exists");

        var targetFile = new File(args[1]);

        try(var input = new Scanner(sourceFile);
            var output = new PrintWriter(targetFile)){
            //input.useDelimiter("\r\n");
            while (input.hasNext()){
                String line = input.nextLine();
                String content = line.replace(args[2], args[3]);
                output.println(content);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
